package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.RoleService;
import com.asm.vo.sys.RoleVo;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author tanzujie
 */
@Api(value = "角色Controller")
@RestController
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;

    /**
     * 分页查询所有角色
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 角色
     */
    @ApiOperation(value = "查询所有角色", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/roles")
    public ResponseEntity<PageResult<RoleVo>> getAllRoles(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", roleService.findAllRoles(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取角色
     *
     * @param id 角色编号
     * @return 角色
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色编号")})
    @GetMapping("/role/{id}")
    public ResponseEntity<Result<RoleVo>> getRoleById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", roleService.findRoleById(id)), HttpStatus.OK);
    }

    /**
     * 根据角色类型代码分页查询角色
     *
     * @param code       角色代码
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 角色
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色代码"),
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/roles/code/{code}")
    public ResponseEntity<PageResult<RoleVo>> getRoleByCode(@PathVariable("code") String code, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", roleService.findRoleByCode(code, pageNumber, pageSize)), HttpStatus.OK);
    }


    /**
     * 增加角色
     *
     * @param roleVo 角色对象
     * @return 操作状态
     */
    @PostMapping("/role/add")
    public ResponseEntity<Result> addRole(@RequestBody @ApiParam(name = "roleVo", value = "角色对象") RoleVo roleVo) {
        roleService.addRole(roleVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新角色
     *
     * @param roleVo 角色对象
     * @return 操作状态
     */
    @PutMapping("/role/update")
    public ResponseEntity<Result> updateRole(@RequestBody @ApiParam(name = "roleVo", value = "角色对象") RoleVo roleVo) {
        roleService.updateRole(roleVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除角色
     *
     * @param id 角色编号
     * @return 角色
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色编号")})
    @DeleteMapping("/role/delete/{id}")
    public ResponseEntity<Result> deleteRole(@PathVariable("id") Long id) {
        roleService.deleteRole(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }
}
